<template>
    <div id="line-bar" ref="canvas" class="canvas-line-bar" :style="{width: '100%', height: canHeight + 'vh'}"></div>
</template>

<script setup lang="ts">
import { defineProps, computed, onMounted } from 'vue'
import * as echarts from 'echarts/core';
import {
  ToolboxComponent,
  ToolboxComponentOption,
  TooltipComponent,
  TooltipComponentOption,
  GridComponent,
  GridComponentOption,
  LegendComponent,
  LegendComponentOption
} from 'echarts/components';
import {
  BarChart,
  BarSeriesOption,
  LineChart,
  LineSeriesOption
} from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

echarts.use([
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  LineChart,
  CanvasRenderer,
  UniversalTransition
]);

type EChartsOption = echarts.ComposeOption<
  | ToolboxComponentOption
  | TooltipComponentOption
  | GridComponentOption
  | LegendComponentOption
  | BarSeriesOption
  | LineSeriesOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})
let canHeight = computed(() => { return props.canvasHeight })
onMounted(() => {
    var chartDom = document.getElementById('line-bar')!;
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;

    option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            },
            backgroundColor: '#15294d',
            borderColor: '#15294d', 
            textStyle: {
                color: '#ffffff',
                fontSize: 12
            }
        },
        toolbox: {
            show: false,
            feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
            }
        },
        grid: {
            top: 50,
            left: 20,
            right: 0,
            bottom: 0,
            containLabel: true
        },
        legend: {
            align: 'left',
            right: 20,
            data: ['增长数', '增长率'],
            textStyle: {color: '#fff'},
        },
        xAxis: [
            {
                type: 'category',
                data: props.data.xAxisDataList || ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                axisPointer: {
                    type: 'shadow'
                },
                axisLabel: {
                    show: true,
                    color: '#c3c6ce'                
                }
            }
        ],
        yAxis: [
            {
                show: true,
                type: 'value',
                // name: 'Precipitation',
                min: 0,
                // max: 250,
                // interval: 50,
                axisLabel: {
                    show: true,
                    color: '#c3c6ce',
                    formatter: '{value}'
                },
                axisLine: {
                    show: true
                },
                axisTick: {
                    show: true
                },
                splitLine:{show:false, lineStyle:{color: 'blue'}},
            },
            {
                show: false,
                type: 'value',
                // name: 'Temperature',
                min: 0,
                // max: 25,
                interval: 5,
                axisLabel: {
                    formatter: '{value}'
                }
            }
        ],
        series: [       
            {
                name: '增长数',
                type: 'line',
                yAxisIndex: 1,
                tooltip: {
                    valueFormatter: function (value) {
                    return (value as number);
                    }
                },
                color: '#ff0000',
                symbolSize: 12,
                lineStyle: {
                    width: 1,
                },
                data: props.data.lineSeriesDataList || [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
            },
            {
                name: '增长率',
                type: 'bar',
                tooltip: {
                    valueFormatter: function (value) {
                    return (value as number);
                    }
                },
                barWidth: 8,
                barMinHeight: 10,
                itemStyle: {
                    color: {
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: '#20d6d3' // 0% 处的颜色
                        }, {
                            offset: 1, color: '#11ce91' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    },
                    borderRadius: 8,

                },
                data: props.data.barSeriesDataList
            },
        ]
    };

    option && myChart.setOption(option);

})
</script>

<style scope>
</style>
